package com.inspur.demo.controller;

import com.inspur.demo.common.CommResultMsg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 *    
 * 控制层
 *
 * @desc:
 * @author: li.peng02
 * @date: 2019/3/13
 * @version: v1.0 
 */
@RestController
public class RibbonController {

    private Logger logger = LoggerFactory.getLogger(RibbonController.class);

    @Resource
    private RestTemplate balanceRestTemplate;

    @Resource
    private LoadBalancerClient loadBalancerClient;

    /**
     * 使用带有负载均衡的RestTemplate和clientName访问生产者
     *
     * @return
     */
    @RequestMapping("/ribbon/balance/test")
    public ResponseEntity<CommResultMsg> test01() {
        ServiceInstance serviceInstance = loadBalancerClient.choose("spring-cloud-producer");
        ResponseEntity<CommResultMsg> forEntity = balanceRestTemplate.getForEntity("http://" + serviceInstance.getServiceId() + "/producer/test?name=123", CommResultMsg.class);
        return forEntity;
    }

}
